package cn.dada.dadarecipes.user.user.controller;

import cn.dada.dadarecipes.base.response.JsonResult;
import cn.dada.dadarecipes.user.user.pojo.dto.*;
import cn.dada.dadarecipes.user.user.pojo.vo.UserVO;
import cn.dada.dadarecipes.user.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "01.01用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    UserService userService;
    @ApiOperation(value = "用户注册功能")
    @PostMapping("/reg")
    public JsonResult reg(UserRegParam userRegParam) {
        log.debug("用户注册:userRegParam={}", userRegParam);
        UserVO userVO = userService.reg(userRegParam);
        return JsonResult.ok(userVO);
    }

    @ApiOperation(value = "用户登录功能")
    @PostMapping("/login")
    public JsonResult login(UserLoginParam userLoginParam, @ApiIgnore HttpSession session) {
        log.debug("用户登录:userLoginParam={}", userLoginParam);
        UserVO userVO = userService.login(userLoginParam, session);
        session.setAttribute("user", userVO);
        return JsonResult.ok(userVO);
    }
    @ApiOperation(value = "用户退出功能")
    @PostMapping("/logout/{id}")
    public JsonResult logout(@PathVariable Long id,@ApiIgnore HttpSession session) {
        log.debug("用户退出:userId={}", id);
        userService.logout(id, session);
        session.removeAttribute("user");
        return JsonResult.ok();
    }
    @ApiOperation(value = "修改密码功能")
    @PostMapping("/password")
    public JsonResult password(UserPasswordParam userPasswordParam) {
        log.debug("修改密码:userId={}", userPasswordParam);
        userService.password(userPasswordParam);
        return JsonResult.ok();
    }
    @ApiOperation(value = "用户注销功能")
    @PostMapping("/logOff/{userId}")
    public JsonResult delete(@PathVariable Long userId) {
        log.debug("用户注销:userId={}", userId);
        userService.delete(userId);
        return JsonResult.ok();
    }

    @ApiOperation(value = "用户基本信息编辑功能")
    @PostMapping("/messageEdit")
    public JsonResult edit(UserMessageEditParam userMessageEditParam) {
        log.debug("用户编辑:userMessageEditParam={}", userMessageEditParam);
        userService.update(userMessageEditParam);
        return JsonResult.ok();
    }
    @ApiOperation(value = "用户基本信息查询功能")
    @GetMapping("/queryMessage/{userId}")
    public JsonResult query(@PathVariable Long userId) {
        log.debug("用户查询:userId={}", userId);
        List<UserVO> userVO = userService.query(userId);
        log.debug("用户查询结果:userVO={}", userVO);
        return JsonResult.ok(userVO);
    }

    @ApiOperation(value = "用户密码重置功能")
    @PostMapping("/resetPassword")
    public JsonResult resetPassword(UserResetPassword userResetPassword){
        log.debug("用户密码重置:userResetPassword={}", userResetPassword);
        userService.resetPassword(userResetPassword);
        return JsonResult.ok();
    }

    @ApiOperation(value = "手机号登录功能")
    @PostMapping("/phoneLogin")
    public JsonResult login(UserPhoneLoginParam userPhoneLoginParam ,@ApiIgnore HttpSession session) {
        log.debug("用户登录:userLoginParam={}", userPhoneLoginParam);
        UserVO userVO = userService.phoneLogin(userPhoneLoginParam, session);
        return JsonResult.ok(userVO);
    }

    @ApiOperation(value = "生成验证码")
    @GetMapping("/generateCaptcha/{phone}")
    public JsonResult generateCaptcha(@PathVariable String phone) {
        String captcha = userService.generateAndStoreCaptcha(phone);
        return JsonResult.ok(captcha);
    }

}